<?php
namespace app\admin\controller;
use app\admin\model\IndexTableModel;
use app\admin\model\JoinTableModel;
use think\Db;
use think\Request;
use app\common\model\User;
use app\common\model\UserLevel;
use app\common\validate\UserValidate;

require_once (__DIR__.'/../../../vendor/ReidsLock.class.php');
class FamemsfbzController extends Controller
{

    /**
     * 音视频价格管理
     * User:@hang
     */
    public function index(Request $request)
    {
        $param = $request->param();
        $whehe = array();
        $whehe[] = ['a.me_sex','=',2];
        if (isset($param['member_id']) && !empty($param['member_id'])) {
            $whehe[] = ['a.member_id','=',$param['member_id']];
        }
        if (isset($param['me_uid']) && !empty($param['me_uid'])) {
            $whehe[] = ['a.me_uid','like',"%{$param['me_uid']}%"];
        }
        if (isset($param['me_phone']) && !empty($param['me_phone'])) {
            $whehe[] = ['a.me_phone','like',"%{$param['me_phone']}%"];
        }
        if (isset($param['me_nick_name']) && !empty($param['me_nick_name'])) {
            $whehe[] = ['a.me_nick_name','like',"%{$param['me_nick_name']}%"];
        }
        $whehetime = '';
        if (isset($param['me_add_time']) && !empty($param['me_add_time'])) {
            $time_arr = explode(' - ', $param['me_add_time']);
            $start_time = strtotime($time_arr[0]);
            $end_time = strtotime($time_arr[1].'23:59:59');
            $whehetime = "a.me_add_time >= $start_time AND a.me_add_time <= $end_time";
        }

        if (isset($param['fmember_id']) && !empty($param['fmember_id'])) {
            $whehe[] = ['b.member_id','=',$param['fmember_id']];
        }
        if (isset($param['fme_uid']) && !empty($param['fme_uid'])) {
            $whehe[] = ['b.me_uid','like',"%{$param['fme_uid']}%"];
        }
        if (isset($param['fme_phone']) && !empty($param['fme_phone'])) {
            $whehe[] = ['b.me_phone','like',"%{$param['fme_phone']}%"];
        }
        if (isset($param['fme_nick_name']) && !empty($param['fme_nick_name'])) {
            $whehe[] = ['b.me_nick_name','like',"%{$param['fme_nick_name']}%"];
        }
        $adminid = $this->uid;

        $Common = New Common();//只能看见自己相关的用户
        $q_member_str = $Common->getMyXmemberIdStr($this->uid);
        $wheheShow = '';
        if(!empty($q_member_str)) {
            $wheheShow = "a.member_id in($q_member_str)";
        }

        $IndexTableModel = New IndexTableModel();
        $data = Db::table('fa_member')
            ->alias('a')
            ->join("fa_member b",'a.me_parent = b.member_id',"LEFT")//父级
            ->join("fa_member_sfbz c",'a.member_id = c.member_id',"LEFT")//配置价格
            ->field("a.*,b.member_id as f_member_id,b.me_uid as f_me_uid,b.me_nick_name as f_me_nick_name,(select count(z.member_id) from fa_member z where z.me_parent=a.member_id) as zj_num,c.yp_money,c.sp_money")
            ->order('a.member_id desc')
            ->where($whehe)
            ->where($whehetime)
            ->where($wheheShow)
            ->paginate($this->admin['per_page'],false,['query'=>request()->param()]);

        $data->each(function ($v, $k)use($IndexTableModel,$adminid) {
            //只有超管能看全部手机号
            if($adminid != 1){
                $v['me_phone'] = hide_phone_number($v['me_phone']);
            }

            if(empty($v['f_member_id'])){
                $v['me_parent_word'] = '未绑定';$v['me_bind_type_word'] = '未绑定';
            }else{
                $v['me_parent_word'] = $v['f_member_id'].'('.$v['f_me_uid'].')('.$v['f_me_nick_name'].')';
                if($v['me_bind_type'] == 1){
                    $v['me_bind_type_word'] = '扫码绑定';
                }else{
                    $v['me_bind_type_word'] = '充值绑定';
                }
            }
            switch($v['me_sex']){
                case '0': $v['me_sex_word'] = '未设置';break;
                case '1': $v['me_sex_word'] = '男';break;
                case '2': $v['me_sex_word'] = '女';break;
            }
            switch($v['me_is_vip']){
                case '0': $v['me_is_vip_word'] = '未开通';break;
                case '1': $v['me_is_vip_word'] = '已开通';break;
            }

            $v['me_status_time_word'] = '-';
            switch($v['me_status']){
                case '1': $v['me_status_word'] = '正常';$v['me_status_time_word'] = '未封禁';break;
                case '2': $v['me_status_word'] = '封禁';
                    if($v['me_status_time'] != -1 && $v['me_status_time'] != 0){
                        $v['me_status_time_word'] = date('Y-m-d H:i:s',$v['me_status_time']);
                    }else{
                        $v['me_status_time_word'] = '永久';
                    }
                    break;
            }

            if(!empty(strpos($v['me_phone'], 'zx'))){
                $v['me_phone'] = explode('zx',$v['me_phone'])[0].'（已注销）';
                $v['me_nick_name'] = explode('zx',$v['me_nick_name'])[0].'（已注销）';
                $v['me_status'] = 99;
                $v['me_status_time_word'] = '已注销';
            }
            $v['me_add_time'] = date('m-d H:i',$v['me_add_time']);
            $v['me_bind_time_word'] = !empty($v['me_bind_time'])?date('m-d H:i',$v['me_bind_time']):'未绑定';

            if(empty($v['yp_money']) || $v['yp_money'] == -1){
                $v['yp_money_word'] = '系统默认';
            }else{
                $v['yp_money_word'] = $v['yp_money'].' 油票';
            }
            if(empty($v['sp_money']) || $v['sp_money'] == -1){
                $v['sp_money_word'] = '系统默认';
            }else{
                $v['sp_money_word'] = $v['sp_money'].' 油票';
            }
            return $v;
        });
        //关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
        ]);
        return $this->fetch();
    }


    /**
     * 调整音视频价格页面
     * User:@hang
     */
    public function getsfbz($id){
        $IndexTableModel = New IndexTableModel();
        $memberInfo = $IndexTableModel->getFind('fa_member',"`member_id` = '{$id}'",'member_id,me_uid,me_nick_name');
        $sfbzInfo = $IndexTableModel->getFind('fa_member_sfbz',"`member_id` = '{$id}'",'*');
        $this->assign([
            'memberinfo' => $memberInfo,
            'member_id' => $id,
            'sfbzinfo' => $sfbzInfo,
        ]);
        return $this->fetch();
    }


    /**
     * 确认调整音视频价格
     * User:@hang
     */
    public function actionsetsfbz(){
        $member_id = input('member_id');
        $yp_money = input('yp_money');//音频价格
        $sp_money = input('sp_money');//视频价格
        //操作上锁
        $Redis_Lock  =  new \Redislock();
        $otc_sell_key  =  'lock:actionsetsfbz'.$member_id;	// 定义锁标识
        $is_lock  =  $Redis_Lock->lock($otc_sell_key, 15);	// 获取锁
        //解锁失败
        if(!$is_lock){
            return admin_error('操作频繁，请稍后再试！',URL_CLOSE_REFRESH);
        }
        if(empty($yp_money)){
            $Redis_Lock->unlock($otc_sell_key);	//释放锁
            return admin_error('音频价格不可为0',URL_CLOSE_REFRESH);
        }
        if(!is_numeric($yp_money)){
            $Redis_Lock->unlock($otc_sell_key);	//释放锁
            return admin_error('音频价格输入错误',URL_CLOSE_REFRESH);
        }
        if(getFloatLength($yp_money) > 2){
            $Redis_Lock->unlock($otc_sell_key);	//释放锁
            return admin_error('音频价格最大两位小数',URL_CLOSE_REFRESH);
        }
        if($yp_money < 0){
            if($yp_money != -1){
                $Redis_Lock->unlock($otc_sell_key);	//释放锁
                return admin_error('音频价格不能为负数',URL_CLOSE_REFRESH);
            }
        }
        if(empty($sp_money)){
            $Redis_Lock->unlock($otc_sell_key);	//释放锁
            return admin_error('视频价格不可为0',URL_CLOSE_REFRESH);
        }
        if(!is_numeric($sp_money)){
            $Redis_Lock->unlock($otc_sell_key);	//释放锁
            return admin_error('视频价格输入错误',URL_CLOSE_REFRESH);
        }
        if(getFloatLength($sp_money) > 2){
            $Redis_Lock->unlock($otc_sell_key);	//释放锁
            return admin_error('视频价格最大两位小数',URL_CLOSE_REFRESH);
        }
        if($sp_money < 0){
            if($sp_money != -1){
                $Redis_Lock->unlock($otc_sell_key);	//释放锁
                return admin_error('视频价格不能为负数',URL_CLOSE_REFRESH);
            }
        }

        $IndexTableModel = New IndexTableModel();
        $memberInfo = $IndexTableModel->getFind('fa_member',"`member_id` = '{$member_id}'","member_id,me_sex");
        if(empty($memberInfo)){
            $Redis_Lock->unlock($otc_sell_key);	//释放锁
            return admin_error('用户信息查询失败',URL_CLOSE_REFRESH);
        }
        if($memberInfo['me_sex'] != 2){
            $Redis_Lock->unlock($otc_sell_key);	//释放锁
            return admin_error('只有女性用户才可调整音视频价格',URL_CLOSE_REFRESH);
        }
        $sfbzInfo = $IndexTableModel->getFind('fa_member_sfbz',"`member_id` = '{$member_id}'",'*');
        if(empty($sfbzInfo)){
            $IndexTableModel->addInfo('fa_member_sfbz',['member_id'=>$member_id,'yp_money'=>$yp_money,'sp_money'=>$sp_money,'add_time'=>time()]);
        }else{
            $IndexTableModel->editInfo('fa_member_sfbz',"`id` = '{$sfbzInfo['id']}'",['yp_money'=>$yp_money,'sp_money'=>$sp_money,'add_time'=>time()]);
        }
        $Redis_Lock->unlock($otc_sell_key);	//释放锁
        return admin_success('调整音视频价格成功', URL_RELOAD);
    }



}
